package com.aaa.lib_java;

import com.aaa.lib_java.android.Base64;

import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;

public class MyClass {

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
//        myClass.RSATest();
        myClass.testBase64();
    }

    public void RSATest() {
        String privateKeyStr = "308204BD020100300D06092A864886F70D0101010500048204A7308204A302010002820101008F7F4DB3E73960B27581486C7A23E315F8E41544141D8945178BFB60114CE6D607904F2014208E04BE7F038FAC7A67286AB73369C5963342854658259E150518B21C80A68BB6561BD2D52D0D79F83F6A6009E13E90E21B65C799283E24E3D1184BE70DC4C1C46D4163BE146D36F5345924DB198C234A494E4C19DDF4471E5FE7B156ED7C7804D5FC938780A4C90629ED979F7612AFE7A0425ECB1AFC7BAFFF12B6A391691961097259C655F8C4C278BCB6AC649C460FEA1AD22CDB0D018DB09E4F12B4ABB4E11FEE6B605CE1A4A56B54EFA7AB7192B939CA4A424CC650CF3105470EE40F53F92C1D49DA96047C7C5A4D17BC033A84B674369B74BD01F7641EC30203010001028201000CACCD28C317A833C08F0308D895BB3FC6C4781FBE251E69BF7035168C6B535D26BD8CF7AE3F72914C6052135FEEA4965C82C279A92320C8FDEBB395330C49447555C62EAFDAE86A91BF69B8D8840B2F4812120176F10F442368230C0631D20232E62855AFB091895142F4C69C304C32443FF9818D8BC8C3853E5302CF960918D4B687C0AC88DBC09C829B260A0781FC18469432D11A2C4B3B1EC0BFE24D761AF7E130F636693E4B83BE5A4E6C776032F920E15EA717BDB15E5D21A83A5403405A09196BDE2364FD19E6B63E5060F3B0C87428315448B7D63FB0B482D29126DEB1A9B92E545B024418E9196A0203D415ED0DD915681BA17063AF3CD15C67EC6502818100C4EBD6C4D3F1CE3EF11294451E1645EB43951424521A6CC80E43CE7F71DF2380F7AF7D2F6A43794D6AC049F2B44368CCD284BF2F45E967C8E0931B1E3DFF870611F3747BEC6F517C3218289CBCC4FCB1CC65A5254136F154A2B780C68374AD7250FEDE77A1982F19AE1F3A8653B3CF1FABEE31B504513A9862473010D6B833D502818100BA8C56B1154C787F0675251191BC3722F5F4A5E6338A1CA5EFE316EBBDBAC1F1F613EB54A2E1E3C056D7D8AD043F7FBF3F00BEA56E4EB3DC1DC1DFB6925E5E59F7A475BC21304C902EB10750B53AC136A51009E724F781750C834ACAFD2E94C4D7E613670EA2A5985A2997562D6B56CE599DBDEB5E4874B4E1D8B37C3E570C3702818008F067C47196957537FE4EEAC04DE488A47DBCF7551ECBF75CE6691651F5FE279996C6C9033AE2E3C7C7C8D392A948B59B8CD91CED40862C913D3C4AE827945A8B3011069B1A0689EBC03AFFBA75FABE9B77B5C2762E67501F3ED342AFAEF93CAA3E979CB8A22EE92C855063CEA2E7F1191DEBF5BAA771507F8D99B9F9F54CAD0281805437D54F7EF116192ABE2A2D036138772518FCE8AF6DF3BF0A4CB865573380298F51B0D575EF8B15A35F30E7F35A037F409F427287192AE3FDDF2FF349A326EF70F8A35A7A95468B7538E0B79409E17E9D085FA025B54391A74382B99F91AF022B0228D246EE30DA56221EBB7E27CE79BC45959F33E27C25C92C3EE3A175450302818100833EF6059B7C47D63D53F812A990E25B4EE35B16FB67C190645528C338ED74CA09C8C105BEBED52D4E1E0445AA712EC9257CAEFD2A1C291CB8BFAB36CF8F3345B9F2466BAE6EFD173C216F6E687E52A4DE52EB9369430D03851695836337011A05E2BC0FAE88D1D5026F4B169F282BF62EBA352C9648A3DED4B5493CA4DB9A6F";
        String publicKeyStr = "30820122300D06092A864886F70D01010105000382010F003082010A02820101008F7F4DB3E73960B27581486C7A23E315F8E41544141D8945178BFB60114CE6D607904F2014208E04BE7F038FAC7A67286AB73369C5963342854658259E150518B21C80A68BB6561BD2D52D0D79F83F6A6009E13E90E21B65C799283E24E3D1184BE70DC4C1C46D4163BE146D36F5345924DB198C234A494E4C19DDF4471E5FE7B156ED7C7804D5FC938780A4C90629ED979F7612AFE7A0425ECB1AFC7BAFFF12B6A391691961097259C655F8C4C278BCB6AC649C460FEA1AD22CDB0D018DB09E4F12B4ABB4E11FEE6B605CE1A4A56B54EFA7AB7192B939CA4A424CC650CF3105470EE40F53F92C1D49DA96047C7C5A4D17BC033A84B674369B74BD01F7641EC30203010001";

        System.out.println("hexString2Bytes加载公私钥");
        byte[] bytes = RSAUtils.hexString2Bytes(privateKeyStr);
        PrivateKey privateKey = null;
        try {
            privateKey = RSAUtils.getPrivateKey(bytes);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            e.printStackTrace();
        }

        bytes = RSAUtils.hexString2Bytes(publicKeyStr);
        PublicKey publicKey = null;
        try {
            publicKey = RSAUtils.getPublicKey(bytes);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            e.printStackTrace();
        }

        System.out.println("hexString2Bytes转换加密后的字符串");
        String string = "74FD22D38997949DCF6A8E0C7373316EAEBD914D120E302F67FCAB383D5CA60C7D5C099301779855EEE5265C4B65F2D157FF1C68248AC501D86F20E311FB993A8B2BD97C8A2037697ABDF6C6A486177851223D6D4E822ABE10119832B8722B76D76B49138B96B33668BCEAC02F03E349868FD9BCECF1ACB7645BCD6C3A43866A0761E11656DBBE356CD5F5314028BDD8D88ACE9B5EDC5F1D815BD5BC480A0FB756355CD724E571F9FE4058FDB4FF72B960B895CB67CC8E4F918F4848BE7EB96E25D49BF1BDC8C5D33AB98CA91508DA039896FC44AE27D294CA1AC48A3D01D1E0427608EFAA6AE7B8AB637BE1F9680EA9C126474982203D6D3EF9CF6374B40475";
        bytes = RSAUtils.hexString2Bytes(string);
        bytes = RSAUtils.decryptDataPublic(bytes, publicKey);
        string = new String(bytes);
        System.out.println("解密=" + string);
    }

    public void testBase64() {
        String src = "010287编解码";
        String des = "MDEwMjg357yW6Kej56CB";

        System.out.println("src=" + src);

        System.out.println("*****\njava.Base64");
        byte[] bytes = com.aaa.lib_java.java.Base64.getEncoder().encode(src.getBytes());
        des = new String(bytes);
        System.out.println("getDecoder().encode=" + des);
        bytes = com.aaa.lib_java.java.Base64.getDecoder().decode(des);
        System.out.println("getDecoder().decode=" + new String(bytes));

        src = "https://www.wanandroid.com/";
        src = "然而，接着发生的事让大家极为震惊。向导抱走幼龟不久，成群的幼龟从巢口鱼贯而出。" +
                "我们很忆明白:我们干了一件愚不可及的蠢事。那只先出来的幼龟，原来是龟群的“侦察兵”，一旦遇到危险，便会返回龟巢。" +
                "那只幼龟被向导引向大海，巢中的幼龟得到错误信息，以为外面很安全，于是争先恐后地结伴而出。";

//        Base64.DEFAULT 默认的编码方式
//        Base64.NO_PADDING 忽略后缀的=号
//        Base64.NO_WRAP 省略换行符
//        Base64.CTRL 使用微软的换行符,而不是Liunux的
//        Base64.URL_SAFE 使用-和_代替+和/以保证在URL的安全.通常用与网络传输

        System.out.println("*****\nandroid.Base64.DEFAULT 默认的编码方式");
        bytes = com.aaa.lib_java.android.Base64.encode(src.getBytes(), com.aaa.lib_java.android.Base64.DEFAULT);
        des = new String(bytes);
        System.out.println("encode=" + des);
        bytes = com.aaa.lib_java.android.Base64.decode(des, com.aaa.lib_java.android.Base64.DEFAULT);
        System.out.println("decode=" + new String(bytes));

        System.out.println("*****\nandroid.Base64.NO_PADDING 忽略后缀的=号");
        bytes = com.aaa.lib_java.android.Base64.encode(src.getBytes(), com.aaa.lib_java.android.Base64.NO_PADDING);
        des = new String(bytes);
        System.out.println("encode=" + des);
        bytes = com.aaa.lib_java.android.Base64.decode(des, com.aaa.lib_java.android.Base64.NO_PADDING);
        System.out.println("decode=" + new String(bytes));

        System.out.println("*****\nandroid.Base64.NO_WRAP 省略换行符");
        bytes = com.aaa.lib_java.android.Base64.encode(src.getBytes(), com.aaa.lib_java.android.Base64.NO_WRAP);
        des = new String(bytes);
        System.out.println("encode=" + des);
        bytes = com.aaa.lib_java.android.Base64.decode(des, com.aaa.lib_java.android.Base64.NO_WRAP);
        System.out.println("decode=" + new String(bytes));

        System.out.println("*****\nandroid.Base64.CRLF 使用微软的换行符,而不是Liunux的");
        bytes = com.aaa.lib_java.android.Base64.encode(src.getBytes(), com.aaa.lib_java.android.Base64.CRLF);
        des = new String(bytes);
        System.out.println("encode=" + des);
        bytes = com.aaa.lib_java.android.Base64.decode(des, Base64.CRLF);
        System.out.println("decode=" + new String(bytes));

        System.out.println("*****\nandroid.Base64.URL_SAFE 使用-和_代替+和/以保证在URL的安全.通常用与网络传输");
        bytes = com.aaa.lib_java.android.Base64.encode(src.getBytes(), com.aaa.lib_java.android.Base64.URL_SAFE);
        des = new String(bytes);
        System.out.println("encode=" + des);
        bytes = com.aaa.lib_java.android.Base64.decode(des, Base64.URL_SAFE);
        System.out.println("decode=" + new String(bytes));

        System.out.println("*****\nandroid.Base64.NO_CLOSE");
        bytes = com.aaa.lib_java.android.Base64.encode(src.getBytes(), com.aaa.lib_java.android.Base64.NO_CLOSE);
        des = new String(bytes);
        System.out.println("encode=" + des);
        bytes = com.aaa.lib_java.android.Base64.decode(des, Base64.NO_CLOSE);
        System.out.println("decode=" + new String(bytes));

        System.out.println("**************************************");
    }
}

